﻿using BasecampRestAPI;
using NUnit.Framework;
using TestBasecampRestAPI.Fakes;

namespace TestBasecampRestAPI
{
	[TestFixture]
	public class TestRestWebService
	{
		const string _url = "https://gumbyinc.basecamphq.com";
		const string _authentication = "Z3VtYnk6Zm9v";
		IRestWebService _camp;
		FakeWebRequestFactory _factory;
		FakeWebRequest _webRequest;

		[SetUp]
		public void SetUp()
		{
			_webRequest = FakeWebRequest.GetInstance();
			_factory = FakeWebRequestFactory.GetInstance();
			_factory.CreateWebRequestFakeResult = _webRequest;
			_camp = RestWebService.GetInstance(_url, "gumby", "foo", _factory);
		}

		[Test]
		public void Construct()
		{
			Assert.IsNotNull(_camp);
			Assert.AreEqual(_url, _camp.Url);
			Assert.AreEqual("gumby", _camp.UserName);
		}

		[Test]
		public void PostRequestGetLocation()
		{
			string requestPath = "projects/2222/todo_lists.xml";
			string data = Properties.Resources.FakeToDoList;
			_webRequest.Location = "/todo_lists/2323.xml";
			string location = _camp.PostRequestGetLocation(requestPath, data);
			Assert.IsNotNull(location);
			Assert.IsNotEmpty(location);
			Assert.AreEqual("/todo_lists/2323.xml", location);
			Assert.IsTrue(_factory.CreateWebRequestCalled);
			Assert.AreEqual(string.Format("{0}/{1}", _url, requestPath), _factory.CreateWebRequestLastUrl);
		}

		[Test]
		public void PutRequest()
		{
			string requestPath = string.Format("todo_items/{0}/complete.xml", 2424);
			_webRequest.ResponseTextGetFakeResult = string.Empty;
			_camp.PutRequest(requestPath, string.Empty);
			Assert.IsTrue(_factory.CreateWebRequestCalled);
			Assert.AreEqual(string.Format("{0}/{1}", _url, requestPath), _factory.CreateWebRequestLastUrl);
			Assert.AreEqual(HttpVerb.Put, _webRequest.MethodSetLastValue);
			Assert.IsTrue(_webRequest.ResponseTextGetCalled);
		}

		[Test]
		public void DeleteRequest()
		{
			string requestPath = ToDoList.PathForId(2424);
			_webRequest.ResponseTextGetFakeResult = string.Empty;
			_camp.DeleteRequest(requestPath);
			Assert.IsTrue(_factory.CreateWebRequestCalled);
			Assert.AreEqual(string.Format("{0}/{1}", _url, requestPath), _factory.CreateWebRequestLastUrl);
			Assert.AreEqual(HttpVerb.Delete, _webRequest.MethodSetLastValue);
			Assert.IsTrue(_webRequest.ResponseTextGetCalled);
		}

		[Test]
		public void AuthenticationFromUserNamePassword()
		{
			Assert.AreEqual(_authentication, RestWebService.AuthenticationFromUserNamePassword("gumby", "foo"));
		}
	}
}
